/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品类型管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="searchForm" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchForm.searchKey" placeholder="搜索关键词"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" :key="randomIndex" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="deptNo" label="鲜花类型编号" width="160">
      </el-table-column>
      <el-table-column sortable prop="deptName" label="类型名称" width="300">
      </el-table-column>
      <el-table-column sortable prop="deptCountNum" label="商品总数" width="200">
      </el-table-column>
      <el-table-column sortable prop="deptCountPrice" label="总价值" width="200">
      </el-table-column>
      <el-table-column sortable prop="editTime" label="修改时间" width="180">
        <template slot-scope="scope">
          <div>{{scope.row.editTime|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="类型名称" prop="deptName">
          <el-input size="small" v-model="editForm.deptName" auto-complete="off" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品总数" prop="deptCountNum">
          <el-input size="small" v-model="editForm.deptCountNum" auto-complete="off" placeholder="请输入商品总数"></el-input>
        </el-form-item>
        <el-form-item label="总价值" prop="deptCountPrice">
          <el-input size="small" v-model="editForm.deptCountPrice" auto-complete="off" placeholder="请输入总价值"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm(editForm)">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        index: '',
        deptName: '',
        deptCountNum: '',
        deptCountPrice: '',
      },
      // rules表单验证
      rules: {
        deptName: [{ required: true, message: '请输入', trigger: 'blur' }],
        deptCountNum: [{ required: true, message: '请输入', trigger: 'blur' }],
        deptCountPrice: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      searchForm: {
        searchKey:''
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      randomIndex:0
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created() {
    this.getdata(this.formInline)
  },
  methods: {
    timeOutHandle(){
      const _this = this
      this.loading = true
      setTimeout(()=>{
        _this.loading = false
      },500)
    },
    getdata(parameter) {      
      let res = {
        code: 0,
        msg: null,
        count: 5,
        data: [
          {
            deptNo:'180001',
            deptName:'梅花',
            deptCountNum:'30',
            deptCountPrice:'120',
            editTime: '2022-01-18',
          },
          {
            deptNo:'180002',
            deptName:'桃花',
            deptCountNum:'21',
            deptCountPrice:'105',
            editTime: '2022-01-19',
          },
          {
            deptNo:'180003',
            deptName:'牡丹',
            deptCountNum:'13',
            deptCountPrice:'91',
            editTime: '2022-01-20',
          },
          {
            deptNo:'180004',
            deptName:'玫瑰',
            deptCountNum:'38',
            deptCountPrice:'190',
            editTime: '2022-01-21',
          },
        ]
      }
      this.timeOutHandle()
      this.listData = res.data
      this.randomIndex = Math.random()
    },
    // 搜索事件
    search() {
      const searchKey = this.searchForm.searchKey
      let data = []
      if(!searchKey) {
        this.getdata()
        return;
      };
      this.listData.map((item,index)=>{
        let keys = Object.keys(item)
        for (let i = 0; i < keys.length; i++) {
          const element = item[keys[i]].toString();
          if(element.indexOf(searchKey) != -1){
            data.push(item)
            break;
          }
        }
      })
      this.listData = data
      this.randomIndex = Math.random()
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.index = index
        this.editForm.deptName = row.deptName
        this.editForm.deptCountNum = row.deptCountNum
        this.editForm.deptCountPrice = row.deptCountPrice
      } else {
        this.title = '添加'
        this.editForm.deptName = ''
        this.editForm.deptType = ''
        this.editForm.deptCountNum = ''
        this.editForm.deptCountPrice = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      const data = {
            deptNo:'12000' + (this.listData.length+1),
            deptName:editData.deptName,
            deptCountNum:editData.deptCountNum,
            deptCountPrice:editData.deptCountPrice,
            editTime: new Date(),
      }
      if(this.title==='修改'){
        this.timeOutHandle()
          this.$set(this.listData,this.editForm.index,data)
      }else{
        this.timeOutHandle()
        this.$set(this.listData,this.listData.length,data)
      }      
      this.editFormVisible = false
    },
    // 删除
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
            this.timeOutHandle()
            this.listData.splice(index,1)   
            this.$message({
              type: 'success',
              message: '已删除'
          }) 
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 